<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 自定义变量 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 自定义变量</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 脚本中使用变量显得脚本更加专业更像是一门语言，变量的作用当然不是为了专业。比如写了有一个长达1000行的 shell 脚本，并且脚本中出现了某一个命令或者路径几百次，如果突然发现不对，想换一下，那不是要更改几百次。当然可以用批量替换的命令，但是也很麻烦，并且脚本显得臃肿。变量的作用就是用来解决这个问题。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># cd /usr/local/sbin/</span></div><div class="line">[root@192 sbin]<span class="comment"># vim variable.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#In this script we will use variables.</span></div><div class="line">d=`date +%H:%M:%S`</div><div class="line"><span class="built_in">echo</span> <span class="string">"The script degin at <span class="variable">$d</span>."</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"Now we'll sleep 2 seconds."</span></div><div class="line">sleep 2</div><div class="line">d1=`date +%H:%M:%S`</div><div class="line"><span class="built_in">echo</span> <span class="string">"The script end at <span class="variable">$d1</span>."</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本中使用到了反引号，单引号的作用是执行命令。‘d’ 和 ‘d1’ 在脚本中作为变量出现。定义变量的格式为： 变量名=变量的值，当在脚本中引用变量时需要加上 ‘$’ 符号。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh variable.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/01.png?raw=true" alt=""></figure></p>
<h2 id="数学运算"><a href="#数学运算" class="headerlink" title="数学运算"></a>数学运算</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 中经常会用到数学运算，下面示例脚本用来计算两个数字的和。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim sum.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#For get the sum of tow numbers.</span></div><div class="line"> </div><div class="line">a=1</div><div class="line">b=2</div><div class="line">sum=$[<span class="variable">$a</span>+<span class="variable">$b</span>]</div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$a</span>+<span class="variable">$b</span>=<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>数学计算要用 [] 括起来并且外头要带一个 ‘$’ </strong> ,脚本结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh sum.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面为几个数学运算相关的例子：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;乘法运算：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh 1.sh</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=3</div><div class="line">b=2</div><div class="line">c=$[<span class="variable">$a</span>*<span class="variable">$b</span>]</div><div class="line"><span class="built_in">echo</span> <span class="variable">$c</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除法运算：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim 2.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">a=10</div><div class="line">b=3</div><div class="line">c=$[<span class="variable">$a</span>/<span class="variable">$b</span>]</div><div class="line"><span class="built_in">echo</span> <span class="variable">$c</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除法运算中 ，c 的结果为 3 ，并不是一个小数，这是因为 shell 默认是不支持小数的。如果想要看小数，需要借助于 bc ，bc 工具是 linux 系统里面的计算器，如果没有就先安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># yum install -y bc</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># echo "scale=2;10/3"|bc</span></div><div class="line">3.33</div></pre></td></tr></table></figure>
<h2 id="和用户交互"><a href="#和用户交互" class="headerlink" title="和用户交互"></a>和用户交互</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 脚本可以实现，让用户输入一些字符串或者让用户去选择的行为。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim read.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#Using 'read' in shell script.</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input a number:"</span> x</div><div class="line"><span class="built_in">read</span> -p <span class="string">"Please input another number:"</span> y</div><div class="line">sum=$[<span class="variable">$x</span>+<span class="variable">$y</span>]</div><div class="line"><span class="built_in">echo</span> <span class="string">"The sum of the two numbers is:<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;read 命令就是用在这样的地方，用于和用户交互。它把用户输入的字符串作为变量值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh read.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加上 -x 选项，再来看看这个执行过程：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x read.sh</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/06.png?raw=true" alt=""></figure></p>
<h2 id="shell-脚本预设变量"><a href="#shell-脚本预设变量" class="headerlink" title="shell 脚本预设变量"></a>shell 脚本预设变量</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有时候会用到这样的命令 /etc/init.d/iptables restart 前面的 /etc/init.d/iptables 文件其实就是一个 shell 脚本，为什么后边可以跟一个 restart ，这里就涉及到了 shell 脚本的 预设变量。实际上， shell 脚本在执行的时候后边是可以跟参数的，而且还可以跟多个。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim option.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">sum=$[<span class="variable">$1</span>+<span class="variable">$2</span>]</div><div class="line"><span class="built_in">echo</span> <span class="string">"sum=<span class="variable">$sum</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x option.sh 1 2</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在脚本中，会觉得奇怪，哪里来的 $1 和 $2 。这就是 shell 脚本的预设变量，其中 $1 的值就是在执行的时候输入的 1 ，而 $2 的值就是在执行的时候输入的 $2 ，当然一个 shell 脚本的预设变量是没有限制的。另外，还有一个 $0  ，不过它代表的是脚本本身的名字。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改一下脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># vim option.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> <span class="variable">$2</span> <span class="variable">$0</span>"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行结果：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 sbin]<span class="comment"># sh -x option.sh 1 2</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/shell%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F/08.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Shell/8. shell 自定义变量/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
